package com.kasax.skin.support.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.Settings;
import android.util.Size;
import android.util.SizeF;
import android.util.SparseArray;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;
import androidx.appcompat.app.AppCompatActivity;

import com.kasax.skin.support.utils.permissions.PermissionConstants;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

public class IntentUtils {
    public static void skip(Context context, Class<? extends Activity> cls) {
        Intent intent = new Intent(context, cls);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 跳转到应用详情
     *
     * @param activity
     */
    public static void goAppDetailsPage(Activity activity) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
    }

    /**
     * 跳转到修改系统设置
     */
    @RequiresPermission(android.Manifest.permission.WRITE_SETTINGS)
    public static void goManageWriteSettings(Activity activity) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));
        if (intent.resolveActivity(activity.getPackageManager()) != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent);
        }
    }

    /**
     * 跳转到AndroidR版本的文件权限申请界面
     */
    public static void goRequestAppFileAccessPermissionPage(Activity activity) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));
        activity.startActivityForResult(intent, PermissionConstants.MANAGE_EXTERNAL_STORAGE_PERMISSON_REQ_CODE);
    }

    /**
     * 处理弹窗权限申请
     *
     * @param activity
     */
    public static void goRequestOverlayManagePage(Activity activity) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(intent);
    }

    public static void forwardActivity(Class<? extends Activity> clazz) {
        forwardActivity(clazz, null);
    }

    public static void forwardActivity(Class<? extends Activity> clazz, Bundle extras) {
        forwardActivity(clazz, extras, null);
    }

    /**
     * 可以并存
     *
     * @param clazz
     * @param extras
     * @param params
     */
    public static void forwardActivity(Class<? extends Activity> clazz, Bundle extras, Map<String, Object> params) {
        Activity activity = CommonUtils.getCurrentShowActivity();
        if (activity != null) {
            Intent intent = new Intent(activity, clazz);
            if (extras != null) {
                intent.putExtras(extras);
            }
            if (params != null) {
                Bundle bundle = fillBundleData(params);
                if (bundle != null) {
                    if (extras != null) {
                        bundle.putAll(extras);
                    }
                    intent.putExtras(bundle);
                }
            }
            activity.startActivity(intent);
        } else {
            Context context = CommonUtils.getContext();
            Intent intent = new Intent(context, clazz);
            if (extras != null) {
                intent.putExtras(extras);
            }
            if (params != null) {
                Bundle bundle = fillBundleData(params);
                if (bundle != null) {
                    if (extras != null) {
                        bundle.putAll(extras);
                    }
                    intent.putExtras(bundle);
                }
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

    public static void forwardCompatActivity(Class<? extends AppCompatActivity> clazz) {
        forwardCompatActivity(clazz, null);
    }

    public static void forwardCompatActivity(Class<? extends AppCompatActivity> clazz, Bundle extras) {
        forwardCompatActivity(clazz, extras, null);
    }

    /**
     * 可以并存
     *
     * @param clazz
     * @param extras
     * @param params
     */
    public static void forwardCompatActivity(Class<? extends AppCompatActivity> clazz, Bundle extras, Map<String,
            Object> params) {
        Activity activity = CommonUtils.getCurrentShowActivity();
        if (activity != null) {
            Intent intent = new Intent(activity, clazz);
            if (extras != null) {
                intent.putExtras(extras);
            }
            if (params != null) {
                Bundle bundle = fillBundleData(params);
                if (bundle != null) {
                    if (extras != null) {
                        bundle.putAll(extras);
                    }
                    intent.putExtras(bundle);
                }
            }
            activity.startActivity(intent);
        } else {
            Context context = CommonUtils.getContext();
            Intent intent = new Intent(context, clazz);
            if (extras != null) {
                intent.putExtras(extras);
            }
            if (params != null) {
                // TODO: 2024-07-21 后续再研究
                Bundle bundle = fillBundleData(params);
                if (bundle != null) {
                    if (extras != null) {
                        bundle.putAll(extras);
                    }
                    intent.putExtras(bundle);
                }
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

    /**
     * 这种方式不行
     * <p>
     *  todo 后面使用到的时候再做优化
     *
     * @param params
     * @return
     */
    @NonNull
    private static Bundle fillBundleData(Map<String, Object> params) {
        Bundle bundle = new Bundle();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            Object value = entry.getValue();
            String key = entry.getKey();
            if (value instanceof Byte) {
                bundle.putByte(key, (Byte) value);
            } else if (value instanceof Character) {
                bundle.putChar(key, (Character) value);
            } else if (value instanceof Short) {
                bundle.putShort(key, (Short) value);
            } else if (value instanceof Float) {
                bundle.putFloat(key, (Float) value);
            } else if (value instanceof CharSequence) {
                bundle.putCharSequence(key, (CharSequence) value);
            } else if (value instanceof Parcelable) {
                bundle.putParcelable(key, (Parcelable) value);
            } else if (value instanceof Size) {
                bundle.putSize(key, (Size) value);
            } else if (value instanceof SizeF) {
                bundle.putSizeF(key, (SizeF) value);
            } else if (value instanceof Parcelable[]) {
                bundle.putParcelableArray(key, (Parcelable[]) value);
            } else if (value instanceof ArrayList) {
                bundle.putParcelableArrayList(key, (ArrayList) value);
            } else if (value instanceof SparseArray) {
                bundle.putSparseParcelableArray(key, (SparseArray) value);
            } else if (value instanceof Serializable) {
                bundle.putSerializable(key, (Serializable) value);
            } else if (value instanceof byte[]) {
                bundle.putByteArray(key, (byte[]) value);
            } else if (value instanceof short[]) {
                bundle.putShortArray(key, (short[]) value);
            } else if (value instanceof char[]) {
                bundle.putCharArray(key, (char[]) value);
            } else if (value instanceof float[]) {
                bundle.putFloatArray(key, (float[]) value);
            } else if (value instanceof CharSequence[]) {
                bundle.putCharSequenceArray(key, (CharSequence[]) value);
            } else if (value instanceof Bundle) {
                bundle.putBundle(key, (Bundle) value);
            } else if (value instanceof Binder) {
                bundle.putBinder(key, (Binder) value);
            } else {
                LogUtils.e("不支持的类型");
            }
            // 通过反映调用putObject
//            try {
//                @SuppressLint("BlockedPrivateApi") Method putObjMethod = bundle.getClass().getDeclaredMethod
//                ("putObject", String.class,
//                        Object.class);
//                if (putObjMethod != null) {
//                    putObjMethod.invoke(bundle, key, value);
//                }
//            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//                e.printStackTrace();
//            }
        }
        return bundle;
    }
}
